package com.id.coinscleaner.adapters

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.id.coinscleaner.bean.ScaredItem
import com.id.coinscleaner.databinding.ItemScaredBinding

/**
 * Created by YiGo on 2022/7/12.
 */
class ScaredAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var data = mutableListOf<ScaredItem>()

    inner class ScaredViewHolder(val binding: ItemScaredBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: ScaredItem) {
            binding.scaredText.text = item.fileName
            binding.scaredText.setTextColor(
                if (item.isRed) Color.parseColor("#D60000") else Color.parseColor(
                    "#FF000000"
                )
            )
            binding.isRedIv.visibility = if (item.isRed) View.VISIBLE else View.GONE
        }

    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return ScaredViewHolder(ItemScaredBinding.inflate(inflater, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = data[position]
        if (holder is ScaredViewHolder) {
            holder.bind(item)
        }
    }

    override fun getItemCount(): Int = data.size

    fun removeItem(vararg indexs: Int) {
        for (index in indexs){
            if (index == -1 || index >= itemCount) return

            data.removeAt(index)
            notifyItemRemoved(index)
            if (index != itemCount) {
                notifyItemRangeChanged(index, itemCount - index)
            }
        }
    }

}